package com.taptech.doufu.ui.adapter.personalcenter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.ui.activity.NovelDetailsActivity;
import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.util.TextUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewPersonalNoteDesActivity;
import com.taptech.doufu.ui.activity.NovelDesToCommActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.viewholder.BaseHolderView;
import com.taptech.doufu.ui.adapter.BaseListAdapter;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.personalcenter.PersonalNotifyBean;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.util.ColorString;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.ui.view.NetworkImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * Company: DiaoBao
 * </p>
 *
 * @author huangzh
 * @date 2014-3-18
 */
public class PersonalNotifyAdapter extends BaseListAdapter {

    Activity activity;
    private List<PersonalNotifyBean> datasList = new ArrayList<PersonalNotifyBean>();

    public PersonalNotifyAdapter(Activity activity) {
        this.activity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.personal_center_adapter_notify, null);
            convertView.setClickable(true);
            holder = new ViewHolder();
            holder.ly = (LinearLayout) convertView.findViewById(R.id.personal_center_news_ly);
            holder.userHeader = (ImageView) convertView.findViewById(R.id.personal_center_notify_user_portrait);
            holder.userName = (TextView) convertView.findViewById(R.id.personal_center_adapter_news_author);
            holder.action = (TextView) convertView.findViewById(R.id.personal_center_adapter_news_action);
            holder.creatTime = (TextView) convertView.findViewById(R.id.personal_center_adapter_news_time);
            holder.commentText = (TextView) convertView.findViewById(R.id.personal_center_notify_comment_text);
            holder.contentGroup = convertView.findViewById(R.id.personal_center_notify_content_group);
            holder.contentText = (TextView) convertView.findViewById(R.id.personal_center_notify_content_text);
            holder.contentImage = (NetworkImageView) convertView.findViewById(R.id.personal_center_notify_content_imgge);
            holder.contentImage.setFullScreen(false);
            convertView.setTag(holder);

        }
        initData(convertView, position);
        return convertView;
    }


    private void initData(View convertView, final int position) {
        try {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            holder.commentText.setVisibility(View.GONE);
            holder.contentText.setVisibility(View.GONE);
            holder.contentImage.setVisibility(View.GONE);
            holder.contentGroup.setVisibility(View.GONE);
            final PersonalNotifyBean data = (PersonalNotifyBean) getDataSource().get(position);
            UserBean user = data.getFrom_user();

            final int opType = DiaobaoUtil.String2Int(data.getOp_type());
            if (user != null) {
                holder.userName.setText(user.getName());
                holder.action.setText(getAction(opType));
                holder.creatTime.setText(DiaobaoUtil.seconds2TimeString(Long.parseLong(data.getAdd_time())));
                holder.userHeader.setImageResource(R.drawable.default_user_portrait);
                GlideUtil.displayCircleImage(holder.userHeader, user.getIcon());
            }
            if (opType == Constant.OP_TYPE_SYSTEM) {
                holder.userName.setTextColor(Color.parseColor(ColorString.RED_TEXT));
            } else {
                holder.userName.setTextColor(Color.parseColor("#808080"));
            }
            // 通知数据
            if (opType == Constant.OP_TYPE_COMMENT || opType == Constant.OP_TYPE_REPLY || opType == Constant.OP_TYPE_REPLY_REPLY || opType == Constant.OP_TYPE_SYSTEM) {
                holder.commentText.setVisibility(View.VISIBLE);
                if (opType == Constant.OP_TYPE_COMMENT)
                    holder.commentText.setText(data.getComment().getComment_content());
                else if (opType == Constant.OP_TYPE_REPLY)
                    holder.commentText.setText(data.getReply().getContent());
                else if (opType == Constant.OP_TYPE_REPLY_REPLY)
                    holder.commentText.setText(data.getReply().getContent());
                else if (opType == Constant.OP_TYPE_SYSTEM)
                    holder.commentText.setText(data.getContent());
            }

            // 内容数据
            if (opType == Constant.OP_TYPE_UGC || opType == Constant.OP_TYPE_COMMENT) {
                HomeTopBean article = data.getArticle();
                if (article != null) {
                    holder.contentGroup.setVisibility(View.VISIBLE);
                    if (!TextUtil.isEmpty(article.getContent())) {
                        holder.contentText.setVisibility(View.VISIBLE);
                        holder.contentText.setText(article.getContent());
                    } else if (!TextUtil.isEmpty(article.getTitle())) {
                        holder.contentText.setVisibility(View.VISIBLE);
                        holder.contentText.setText(article.getTitle());
                    }

                    if (article.getImages() != null && article.getImages().length > 0) {
                        holder.contentImage.setVisibility(View.VISIBLE);
                        holder.contentImage.setImageResource(R.drawable.bg_home_photo_default);
                        try {
                            holder.contentImage.setURL(article.getImages()[0].getImgUrl());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            } else if (opType == Constant.OP_TYPE_REPLY) {
                if (!TextUtil.isEmpty(data.getComment().getComment_content())) {
                    holder.contentGroup.setVisibility(View.VISIBLE);
                    holder.contentText.setVisibility(View.VISIBLE);
                    holder.contentText.setText(data.getComment().getComment_content());
                }
            } else if (opType == Constant.OP_TYPE_REPLY_REPLY) {
                if (!TextUtil.isEmpty(data.getTo_reply().getContent())) {
                    holder.contentGroup.setVisibility(View.VISIBLE);
                    holder.contentText.setVisibility(View.VISIBLE);
                    holder.contentText.setText(data.getTo_reply().getContent());
                }
            }

            if (opType == Constant.OP_TYPE_INVITE_POINT) {
                holder.userHeader.setImageResource(R.drawable.point_flag);
                holder.userName.setText("积分获得");
                holder.commentText.setVisibility(View.VISIBLE);
                holder.commentText.setText(data.getContent());
            }

            if (user.getUserId().equals("0")) {// 系统通知
                holder.userHeader.setImageResource(R.drawable.new_icon);
                holder.userName.setText("系统通知");
                holder.commentText.setText(data.getContent());
                holder.commentText.setVisibility(View.VISIBLE);
            }
            // 由于后台数据里逻辑存在问题，导致无法区分圈子回复和小说回复的类型，先不处理这一部分
            /*
             * holder.ly.setOnClickListener(new OnClickListener() {
             *
             * @Override public void onClick(View arg0) { // TODO Auto-generated
             * method stub if (opType ==
             * Constant.OP_TYPE_COMMENT&&DiaobaoUtil.String2Int
             * (data.getObject_type())==Constant.OP_TYPE_UGC) { if
             * (data.getArticle() == null) { return; } Intent intent = new
             * Intent(activity, NovelDesToCommActivity.class);
             * intent.putExtra(Constant.NOVEL_HOME_BEAN, data.getArticle());
             * intent.putExtra(Constant.TOPIC, data.getArticle().getTopicId());
             * intent.putExtra(Constant.ARTICLE_ID, data.getArticle().getId());
             * intent.putExtra(Constant.FLOWERSHOWFLAG, Constant.FLOWER_SHOW);
             * activity.startActivity(intent); // startCommentActivity(); }
             *
             * } });
             */
            holder.ly.setOnClickListener(new OnClickListener()
                    //holder.commentText.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    int target_type = DiaobaoUtil.String2Int(data.getTarget_type());
                    TTLog.i("TAG", "target_type:" + target_type);
                    if (data.getSubobject_id() != null || data.getObject_id() != null) {
                        switch (target_type) {
                            case PersonalNewDynamicAdapter.TARGET_TYPE_NOVEL_DES:
                                NovelDetailsActivity.Companion.startActivity(activity,data.getObject_id(),Intent.FLAG_ACTIVITY_NEW_TASK);
                                break;
                            case PersonalNewDynamicAdapter.TARGET_TYPE_NOVEL_SELECT:
                            case PersonalNewDynamicAdapter.TARGET_TYPE_NOVEL_SELECT_COMMENT:
                                intent.setClass(activity, NovelDesToCommActivity.class);
                                intent.putExtra(Constant.TOPIC, data.getObject_id());
                                intent.putExtra(Constant.ARTICLE_ID, data.getSubobject_id());
                                // intent.putExtra(Constant.ARTICLE_TITLE,
                                // data.getSubobject_title());
                                intent.putExtra(Constant.FLOWERSHOWFLAG, Constant.FLOWER_HIDE);
                                intent.putExtra(Constant.FLAG, true);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                activity.startActivity(intent);
                                break;
                            case PersonalNewDynamicAdapter.TARGET_TYPE_SWEEP_COMMENT:
                            case PersonalNewDynamicAdapter.TARGET_TYPE_SWEEP_FLOWER:
                                HashMap<String, String> map1 = new HashMap<>();
                                map1.put("id", data.getObject_id());
                                SimpleWeexActivity.startActivity(activity, "vue/articalComment/TFArticalCommentDetail.js", map1, Intent.FLAG_ACTIVITY_NEW_TASK);
                                break;
                            case PersonalNewDynamicAdapter.TARGET_TYPE_NOTE_COMMENT:
                                intent.setClass(activity, SimpleWeexActivity.class);
                                intent.putExtra(SimpleWeexActivity.INTENT_WEEX_URL_KEY, "vue/find/TopicDetail/TFTopicDetail.js");
                                HashMap<String, String> map = new HashMap<>();
                                map.put("id", data.getObject_id());
                                intent.putExtra(SimpleWeexActivity.INTENT_WEEX_DATA_KEY, map);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                activity.startActivity(intent);
                                break;
                            case PersonalNewDynamicAdapter.TARGET_TYPE_DRAW_COMMENT:
                            case PersonalNewDynamicAdapter.TARGET_TYPE_COS_COMMENT:
                                intent.setClass(activity, NewPersonalNoteDesActivity.class);
                                intent.putExtra(Constant.OBJECT_TYPE, target_type + "");
                                intent.putExtra(Constant.ARTICLE_ID, data.getObject_id());
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                activity.startActivity(intent);
                                break;
                            case Constant.OBJECT_TYPE_CARTOON:
                                CartoonServices.enterCartoonDes(activity, data.getObject_id());
                                break;
                            default:
                                break;
                        }
                    } else if (target_type == PersonalNewDynamicAdapter.TARGET_TYPE_NOVEL_SELECT || target_type == PersonalNewDynamicAdapter.TARGET_TYPE_NOVEL_SELECT_COMMENT) {
                        intent.setClass(activity, NovelDesToCommActivity.class);
                        intent.putExtra(Constant.TOPIC, data.getTopic().getId());
                        if (data.getArticle() != null) {
                            intent.putExtra(Constant.ARTICLE_ID, data.getArticle().getId());
                        }
                        intent.putExtra(Constant.FLOWERSHOWFLAG, Constant.FLOWER_HIDE);
                        intent.putExtra(Constant.FLAG, true);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        activity.startActivity(intent);
                    } else if (data.getArticle() != null) {
                        if (isDelComment(data.getArticle().getStatus())) {
                            UIUtil.toastMessage(v.getContext(), "评论已删除");
                            return;
                        }
                        DiaobaoUtil.contentClick(activity, data.getArticle());
                        // intent.setClass(activity, SingleTopicActivity.class);
                        // intent.putExtra(Constant.ARTICLE_ID,
                        // DiaobaoUtil.String2Int(data.getComment().getArticle_id()));
                        // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        // activity.startActivity(intent);
                    } else if (data.getComment() != null) {
                        if (isDelComment(data.getComment().getStatus())) {
                            UIUtil.toastMessage(v.getContext(), "评论已删除");
                            return;
                        }
                        intent.setClass(activity, NovelDesToCommActivity.class);
                        intent.putExtra(Constant.TOPIC, data.getComment().getTopic_id());
                        intent.putExtra(Constant.ARTICLE_ID, DiaobaoUtil.String2Int(data.getComment().getArticle_id()));
                        intent.putExtra(Constant.FLOWERSHOWFLAG, Constant.FLOWER_HIDE);
                        intent.putExtra(Constant.FLAG, true);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        activity.startActivity(intent);
                    }

                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getAction(int opType) {
        String action = "";
        switch (opType) {
            case Constant.OP_TYPE_COMMENT:
                action = "评论了我";
                break;
            case Constant.OP_TYPE_REPLY:
            case Constant.OP_TYPE_REPLY_REPLY:
                action = "回复了我";
                break;
            case Constant.OP_TYPE_REVIEW:
                action = "发布的";
                break;
        }
        return action;
    }

    protected class ViewHolder extends BaseHolderView {
        NetworkImageView contentImage;
        ImageView userHeader;
        TextView userName, action, creatTime, commentText, contentText;
        View contentGroup;
        LinearLayout ly;
    }

    private boolean isDelComment(String status) {
        return "-1".equals(status);
    }

    public void setDataSourceNew(List<PersonalNotifyBean> mainList) {
        // TODO Auto-generated method stub
        this.datasList = mainList;

    }
}
